﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.AuditLogging.LogEntry;
using System.Net;

namespace Sinacor.AuditLogging.LogEntry
{
    [Serializable]
    public abstract class SinacorBaseLogEntry : BaseLogEntry
    {
        private const int LOG_SOURCEMACHINE_DB_SIZE = 100;//VARCHAR
        private const int LOG_COMPANYID_DB_SIZE = 5;//NUMBER
        private const int LOG_MODULEID_DB_SIZE = 5;//VARCHAR
        private const int LOG_USERID_DB_SIZE = 6;//NUMBER

        #region Propriedade Preenchidas automaticamente no construtor
        /// <summary>
        /// Data/Hora da geração do log
        /// Propriedade preenchida automaticamente
        /// </summary>
        public DateTime Timestamp
        {
            get;
            internal set;
        }

        /// <summary>
        /// Nome da máquina onde o log foi gerado
        /// Propriedade preenchida automaticamente
        /// </summary>
        private string _sourceMachine;
        public string SourceMachine
        {
            get
            {
                return _sourceMachine;
            }
            internal set
            {
                if (value != null && value.Length > LOG_SOURCEMACHINE_DB_SIZE)
                    throw new ArgumentException(string.Format("SourceMachine length must be less than {0}.", (1 + LOG_SOURCEMACHINE_DB_SIZE).ToString()));

                _sourceMachine = value;
            }
        }

        /// <summary>
        /// Id da empresa corrente 
        /// </summary>
        private int _companyId;
        public int CompanyId
        {
            get
            {
                return _companyId;
            }
            set
            {
                if (value.ToString().Length > LOG_COMPANYID_DB_SIZE)
                    throw new ArgumentException(string.Format("CompanyId length must be less than {0}.", (1 + LOG_COMPANYID_DB_SIZE).ToString()));

                _companyId = value;
            }
        }

        /// <summary>
        /// Id do módulo corrente
        /// </summary>
        private string _moduleId;
        public string ModuleId
        {
            get
            {
                return _moduleId;
            }
            set
            {
                if (value != null && value.Length > LOG_MODULEID_DB_SIZE)
                    throw new ArgumentException(string.Format("ModuleId length must be less than {0}.", (1 + LOG_MODULEID_DB_SIZE).ToString()));

                _moduleId = value;
            }
        }

        /// <summary>
        /// Id do usuário corrente
        /// </summary>
        private int _userId;
        public int UserId
        {
            get
            {
                return _userId;
            }
            set
            {
                if (value.ToString().Length > LOG_USERID_DB_SIZE)
                    throw new ArgumentException(string.Format("UserId length must be less than {0}.", (1 + LOG_USERID_DB_SIZE).ToString()));

                _userId = value;
            }
        }

        #endregion



        public SinacorBaseLogEntry()
        {
            this.SourceMachine = GetCurrentSourceMachine();
            this.Timestamp = DateTime.Now;
        }

        private string GetCurrentSourceMachine()
        {
            return Dns.GetHostEntry(string.Empty).HostName;
        }
    }
}
